const webpack = require('webpack')

const path = require("path");

const CompressionWebpackPlugin = require('compression-webpack-plugin')

const productionGzipExtensions = ['js', 'css', 'ts']

const TerserPlugin = require('terser-webpack-plugin')

module.exports = {
  outputDir: 'dist',
  publicPath: './',
  productionSourceMap: false,
  runtimeCompiler: true,
  devServer: {
    host: '0.0.0.0',
    open: true,
    port: 9001,
    compress: true
  },
  chainWebpack: config => {
    config.resolve.alias.set("@", path.resolve(__dirname, "./src"));
    //配置txt 和xml 文件的加载
    config.module
      .rule(['txt', 'xml'])
      .test(/\.(txt|xml)$/)
      .use('raw-loader')
      .loader('raw-loader')
      .end()
  },
  configureWebpack: {
    resolve: {
      extensions: [".ts", ".tsx", ".js", ".json", ".vue"]
    },
    module: {
      rules: [{
          test: /\.tsx?$/,
          loader: 'ts-loader',
          exclude: /node_modules/,
          options: {
            appendTsSuffixTo: [/\.vue$/]
          }
        },
        {
          test: /\.js$/,
          exclude: /node_modules/,
          loader: "babel-loader"
        }
      ]
    },
    plugins: [
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "windows.jQuery": "jquery"
      }),
      new CompressionWebpackPlugin({
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'), //匹配文件名
        threshold: 10240, //对10K以上的数据进行压缩
        minRatio: 0.8,
        deleteOriginalAssets: false //是否删除源文件
      }),
      // new TerserPlugin({
      //   sourceMap: false,
      //   terserOptions: {
      //     compress: {
      //       drop_console: true
      //     }
      //   }
      // })

    ]
  },
  transpileDependencies: [/node_modules[/\\\\](element-ui|vuex)[/\\\\]/]

}